package com.cans.lightning.business.lowcode.service.impl;

import com.cans.lightning.base.mapper.IBaseMapper;
import com.cans.lightning.base.service.impl.BaseServiceImpl;
import com.cans.lightning.business.lowcode.dao.AppTableFieldDao;
import com.cans.lightning.business.lowcode.dto.AppTableFieldDto;
import com.cans.lightning.business.lowcode.entity.AppTableField;
import com.cans.lightning.business.lowcode.mapper.AppTableFieldMapper;
import com.cans.lightning.business.lowcode.service.api.IAppTableFieldService;
import com.google.common.collect.Lists;
import org.beetl.sql.core.page.PageResult;
import org.beetl.sql.mapper.BaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 应用信息
 *
 * @author shencan
 * @date 2020/6/14 20:53
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AppTableFieldServiceImpl extends BaseServiceImpl<AppTableField, AppTableFieldDto, String> implements IAppTableFieldService {

    @Resource
    private AppTableFieldDao appTableFieldDao;
    @Resource
    private AppTableFieldMapper appTableFieldMapper;


    @Override
    public BaseMapper<AppTableField> getDaoImpl() {
        return appTableFieldDao;
    }

    @Override
    public IBaseMapper<AppTableField, AppTableFieldDto, String> getMapperImpl() {
        return appTableFieldMapper;
    }

    @Override
    public void saveOrUpdate(List<AppTableFieldDto> fieldArr) {
        if(CollectionUtils.isEmpty(fieldArr)){
            return;
        }
        long sortId = 0;
        for (AppTableFieldDto appTableFieldDto : fieldArr) {
            if(appTableFieldDto.getDelete()){
                this.deleteById(appTableFieldDto.getId());
            }else{
                appTableFieldDto.setSortId(sortId++);
                this.saveOrUpdateDto(appTableFieldDto);
            }
        }
    }

    @Override
    public List<AppTableFieldDto> getByFormId(String formId) {
        return appTableFieldMapper.toDtos(appTableFieldDao.createLambdaQuery().andEq(AppTableField::getFormId, formId).orderBy(AppTableField::getSortId).select());
    }

    @Override
    public List<AppTableFieldDto> getByTableIds(List<String> tableIdList) {
        if(CollectionUtils.isEmpty(tableIdList)){
            return Lists.newArrayList();
        }
        return appTableFieldMapper.toDtos(appTableFieldDao.createLambdaQuery().andIn(AppTableField::getTableId, tableIdList).orderBy(AppTableField::getSortId).select());
    }

    @Override
    public List<AppTableFieldDto> getByTableId(String tableId) {
        List<AppTableField> tableFields = appTableFieldDao.createLambdaQuery().andEq(AppTableField::getTableId, tableId).asc(AppTableField::getSortId).select();
        return appTableFieldMapper.toDtos(tableFields);
    }

    @Override
    public String getNextFieldIndex(String tableId) {
        PageResult<AppTableField> page = appTableFieldDao.createLambdaQuery().andEq(AppTableField::getTableId, tableId).asc(AppTableField::getFieldIndex).page(0, 1);
        List<AppTableField> list = page.getList();
        if(CollectionUtils.isEmpty(list)){
            return "0001";
        }
        AppTableField appTableField = list.get(0);
        return  String.format("%04d", appTableField.getFieldIndex());
    }
}
